package com.nuoyuan.wx.service;

import com.aliyun.oss.model.OSSObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoyuan.wx.common.utils.DateUtil;
import com.nuoyuan.wx.common.utils.GsonUtil;
import com.nuoyuan.wx.vo.FileVo;
import com.nuoyuan.wx.vo.PageVo;
import com.nuoyuan.wx.vo.ProductVo;
import com.nuoyuan.wx.common.aop.ProxyExceptionReturn;
import com.nuoyuan.wx.common.config.OssConfig;
import com.nuoyuan.wx.common.constant.OssFileDirEnum;
import com.nuoyuan.wx.common.dto.BaseRespDto;
import com.nuoyuan.wx.common.utils.OssClientUtil;
import com.nuoyuan.wx.dao.mapper.ProductMapper;
import com.nuoyuan.wx.dao.po.ProductPo;
import com.nuoyuan.wx.service.helper.ProductBeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package com.nuoyuan.wx.service
 * @ClassName ProductService
 * @Description TODO
 * @Author linus
 * @Date 2020-02-16 14:57
 * @Version 1.0
 **/
@Service
public class ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OssConfig ossConfig;

    @ProxyExceptionReturn
    public BaseRespDto add(ProductVo product) {
        productMapper.insert(ProductBeanHelper.toPo(product));
        return BaseRespDto.buildSuccResp();
    }

    @ProxyExceptionReturn
    public BaseRespDto<ProductVo> getDetail(ProductVo product) {
        Wrapper<ProductPo> queryWrapper = new QueryWrapper<>();
        ((QueryWrapper<ProductPo>) queryWrapper).eq("product_id", product.getProductId());
        ProductPo po = productMapper.selectOne(queryWrapper);

        BaseRespDto resp = BaseRespDto.buildSuccResp();
        if(po != null) {
            resp.setData(ProductBeanHelper.toVo(po));
        }

        return resp;
    }

    @ProxyExceptionReturn
    public BaseRespDto update(ProductVo product) {
        Wrapper<ProductPo> updateWrapper = new UpdateWrapper<>();
        ((UpdateWrapper<ProductPo>) updateWrapper).eq("product_id", product.getProductId());
        productMapper.update(ProductBeanHelper.toPo(product), updateWrapper);
        return BaseRespDto.buildSuccResp();
    }

    @ProxyExceptionReturn
    public BaseRespDto<PageVo<ProductVo>> getList(PageVo page) {
        Wrapper<ProductPo> queryWrapper = new QueryWrapper<>();
        ((QueryWrapper<ProductPo>) queryWrapper).orderByDesc("product_id");
        IPage p = new Page();
        p.setCurrent(page.getPageNo());
        p.setSize(page.getPageSize());
        IPage<ProductPo> pages = productMapper.selectPage(p, queryWrapper);

        BaseRespDto resp = BaseRespDto.buildSuccResp();
        if(null == pages || CollectionUtils.isEmpty(pages.getRecords())) {
            PageVo emptyP = new PageVo<>();
            emptyP.setItems(new ArrayList());
            resp.setData(emptyP);
            return resp;
        }

        List<ProductPo> pos = pages.getRecords();
        page.setCount(pages.getTotal());
        page.setItems(ProductBeanHelper.toVos(pos));

        resp.setData(page);
        return resp;
    }


    @ProxyExceptionReturn
    public BaseRespDto<PageVo<ProductVo>> getList(PageVo page, Wrapper<ProductPo> wrapper) {
        IPage p = new Page();
        p.setCurrent(page.getPageNo());
        p.setSize(page.getPageSize());
        IPage<ProductPo> pages = productMapper.selectPage(p, wrapper);

        BaseRespDto resp = BaseRespDto.buildSuccResp();
        if(null == pages || CollectionUtils.isEmpty(pages.getRecords())) {
            PageVo emptyP = new PageVo<>();
            emptyP.setItems(new ArrayList());
            resp.setData(emptyP);
            return resp;
        }

        List<ProductPo> pos = pages.getRecords();
        page.setCount(pages.getTotal());
        page.setItems(ProductBeanHelper.toVos(pos));

        resp.setData(page);
        return resp;
    }



    @ProxyExceptionReturn
    public BaseRespDto<String> upload(FileVo file) {
        String fileUlr = OssClientUtil.upload(ossConfig, file.getFile(), OssFileDirEnum.PRODUCT.name());
        BaseRespDto resp = BaseRespDto.buildSuccResp();
        resp.setData(fileUlr);
        return resp;

    }

    public List<ProductPo> getExpires() {
        Wrapper<ProductPo> queryWrapper = new QueryWrapper<>();
        ((QueryWrapper<ProductPo>) queryWrapper).lt("expireation_date", DateUtil.getCurrentTimestamp());
        return productMapper.selectList(queryWrapper);
    }


    public OSSObject getDownloadFile(String objectName) {
        return OssClientUtil.getObject(ossConfig, objectName);
    }
}
